package com.byCreation.Control;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.aretha.slidemenu.SlideMenu;
import com.byCreatetion.adabter.MainmenuListAdapter;
import com.byCreation.t_globe.ContentActivity;
import com.byCreation.t_globe.R;



import Myclass.ConnectInternet;
import Parser.HttpGetJson;
import Parser.SendToBD;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import animation.ResizeWidthAnimation;

public class SliderMenuLeft implements OnClickListener {
	
	private SlideMenu MSlideMenu;
	public Activity THIS;
	  
	// ==================Main Menu=======================
			public   ListView Mainmenu_lstView;
			public   MainmenuListAdapter Mainmenu_adapter;
			public static  ArrayList<HashMap<String, Object>> Mainmenu_MyArrList;
			public   HashMap<String, Object> Mainmenu_map = new HashMap<String, Object>();
			public   ProgressDialog mProgressDialog;
			public   TextView Mainmenu_txt_title_menu;
			public   LayoutParams Mainmenu_lstView_params;
			
			
		
			public static  String Mainmenu_page_title="เกาะพะงัน";
			public static  String Content_title="เกาะพะงัน";
			
			
			
			public static  String Mainmenu_Data_Dafalt="";
			public static  ArrayList<MenuData> Mainmenu_List_DATA_History = new ArrayList<MenuData>();
			public static  ArrayList<String> Mainmenu_List_Menu_Title = new ArrayList<String>();
			
			
			public  String information_page_title="t-Globe Highlights";
			public  String information_Data_Dafalt="";
			public  ArrayList<MenuData> information_List_DATA_History = new ArrayList<MenuData>();
			public  ArrayList<String> information_List_Menu_Title = new ArrayList<String>();
			
			
			
			
	private boolean iSReset=true;		
			
	
	public SliderMenuLeft(Activity C,boolean reset,SlideMenu mSlideMenu)
	
	{
		
		
		
		iSReset=reset;
		Log.e("Tglobe","iSReset:"+iSReset);
		 THIS=C;
		 MSlideMenu=mSlideMenu;
		 SetMainMenu();
		 Setinformation();
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	public void SetMainMenu()
	{
	
		

		
		
		Mainmenu_lstView = (ListView)THIS.findViewById(R.id.list_view);
		Mainmenu_lstView_params = Mainmenu_lstView.getLayoutParams();
		THIS.setProgressBarIndeterminateVisibility(true);
		Mainmenu_lstView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
				// TODO Auto-generated method stub
				

				if(	Mainmenu_MyArrList.get(position).get("Data").toString().equals("")||
						Mainmenu_MyArrList.get(position).get("Data").toString().equals("[]")	)
				{
					//Toast.makeText(ContentActivity.this, holder.txt_title.getText()+" ID=:"+MyArrList.get(position).get("Id"),100).show();
					Control.page_id=Mainmenu_MyArrList.get(position).get("Id").toString();
					Mainmenu_List_Menu_Title.add(Mainmenu_MyArrList.get(position).get("Title").toString());
					
					if(new ConnectInternet(THIS).checkInternetConnection())
					{
						Content_title=Mainmenu_MyArrList.get(position).get("Title").toString();
						 THIS.startActivity(new Intent(THIS, ContentActivity.class));
						 THIS.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

						 Handler handler=new Handler();
						 Runnable r=new Runnable()
						 {
						     public void run() 
						     {
						    	 MSlideMenu.close(true); 		
						     }
						 };
						 handler.postDelayed(r, 0);
						
					}
				}
				else
				{
					
					Mainmenu_List_Menu_Title.add(Mainmenu_MyArrList.get(position).get("Title").toString());
					Add_ChildToList(Mainmenu_MyArrList.get(position).get("Data").toString(),true);
				}
				
				
				if(Mainmenu_List_DATA_History.size()<=1)
					 THIS.findViewById(R.id.img_back_list).setVisibility(View.INVISIBLE);
					else
						THIS.findViewById(R.id.img_back_list).setVisibility(View.VISIBLE);
				
				
				Log.e("Tglobe", "Control.Mainmenu_List_DATA_History.size():"+Mainmenu_List_DATA_History.size());

			}

		});
		 //=====================ProgressDialog==========================
        mProgressDialog = new ProgressDialog(THIS);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);         
        mProgressDialog.setMessage("Loading......");                       
        mProgressDialog.setCancelable(true);  

        
    
       
       
       
        Mainmenu_txt_title_menu=(TextView)THIS.findViewById(R.id.txt_title_menu);
       
 
        
        Mainmenu_List_Menu_Title.add(Mainmenu_page_title);
        
        
        
        
        if(Mainmenu_List_DATA_History.size()<=1)
        	THIS.findViewById(R.id.img_back_list).setVisibility(View.INVISIBLE);
        else
        	THIS.findViewById(R.id.img_back_list).setVisibility(View.VISIBLE);
        
        
        THIS.findViewById(R.id.img_back_list).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				

				
				if(Mainmenu_List_DATA_History.size()>1)
				{
					THIS.findViewById(R.id.img_back_list).setVisibility(View.VISIBLE);
					Log.e("TG", "size : "+Mainmenu_List_DATA_History.size());
					Mainmenu_List_DATA_History.remove(Mainmenu_List_DATA_History.size()-1);


					
					
					
					Mainmenu_List_Menu_Title.remove(Mainmenu_List_Menu_Title.size()-1);
					Log.e("TG", "Type : "+Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Type);
					
					if(Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Type.equals("child"))
						{
						
						Add_ChildToList(Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Data,false);
						}
					else
					{
						
						Add_MainMenuToList(Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Data,false);
					}
					

				}
				
				if(Mainmenu_List_DATA_History.size()<=1)
					THIS.findViewById(R.id.img_back_list).setVisibility(View.INVISIBLE);
				
				
				Log.e("Tglobe", "size : "+Mainmenu_List_Menu_Title.size());
				Log.e("Tglobe", "Control.Mainmenu_List_DATA_History.size():"+Mainmenu_List_DATA_History.size());

			}
		});
        
        
        
        
        
        
        
        Mainmenu_MyArrList = new ArrayList<HashMap<String, Object>>();
		
		if(new ConnectInternet(THIS).checkInternetConnection())
		{
			
				Log.e("tglobe", "===========LoadData_MainMenu().execute()=========");
				new LoadData_MainMenu().execute();
			
			//new LoadData_information().execute();
		}
		
		
	}
	public void ReSetMainMenu()
	{
	
		Log.e("Tglobe", "Mainmenu_List_Menu_Title : "+Mainmenu_List_Menu_Title.size());
		Log.e("Tglobe", "Mainmenu_List_DATA_History:"+Mainmenu_List_DATA_History.size());
		
		
		if(Mainmenu_List_DATA_History.size()>1)
		{
			THIS.findViewById(R.id.img_back_list).setVisibility(View.VISIBLE);
			
			if(Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Type.equals("child"))
				{
				
				Add_ChildToList(Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Data,false);
				}
			else
			{
				
				Add_MainMenuToList(Mainmenu_List_DATA_History.get(Mainmenu_List_DATA_History.size()-1).Data,false);
			}
			

		}
		
		if(Mainmenu_List_DATA_History.size()<=1)
			THIS.findViewById(R.id.img_back_list).setVisibility(View.INVISIBLE);
		
		
		Log.e("Tglobe", "Mainmenu_List_Menu_Title : "+Mainmenu_List_Menu_Title.size());
		Log.e("Tglobe", "Mainmenu_List_DATA_History:"+Mainmenu_List_DATA_History.size());
		
		
	}
	
	
	class LoadData_MainMenu extends AsyncTask<Object, Integer, Object> {
		
		
		
		

		private boolean ONcancle=false;
		

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			ONcancle=true;
			super.onCancelled();
		}

		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			try {
				ArrayList<String> arrayList_parametor = new ArrayList<String>();
				ArrayList<String> arrayList_value = new ArrayList<String>();

				arrayList_parametor.add("id");
				arrayList_value.add(Control.page_id);

				SendToBD sd = new SendToBD(THIS);
				Mainmenu_Data_Dafalt = sd.send("", "", Control.API_main_menu,arrayList_parametor, arrayList_value);
				Log.e("evt", "API_main_menu=" + Mainmenu_Data_Dafalt + "");

				JSONObject jsonObj = null;
				jsonObj = new JSONObject(Mainmenu_Data_Dafalt);
				JSONArray jsonarray_information = jsonObj.getJSONArray("information");

				// ===========Add to History
				MenuData menudata = new MenuData();
				menudata.Data = jsonObj.getJSONArray("information").toString();
				menudata.Type = "main";
				
				
				
				Mainmenu_List_DATA_History.add(menudata);

				for (int i = 0; i < jsonarray_information.length(); i++) {

					JSONObject jsonObj_information = jsonarray_information
							.getJSONObject(i);

					
					Mainmenu_map = new HashMap<String, Object>();
					Mainmenu_map.put("Title", jsonObj_information.getString("main_menu"));
					Mainmenu_map.put("Id", jsonObj_information.getString("page_id"));
					Mainmenu_map.put("Stuck", "main_menu");

					try {
						JSONArray jsonarray_main_menu_child = jsonObj_information
								.getJSONArray("main_menu_child");
						if (jsonarray_main_menu_child.length() > 0)
							Mainmenu_map.put("Type", "main");
						else
							Mainmenu_map.put("Type", "child");

						Mainmenu_map.put("Data", jsonarray_main_menu_child.toString());

					} catch (Exception ex) {
						Mainmenu_map.put("Type", "child");
						Mainmenu_map.put("Data", "");
					}

					Mainmenu_MyArrList.add(Mainmenu_map);
				
				                // This code will always run on the UI thread, therefore is safe to modify UI elements.
					THIS.runOnUiThread(new Runnable() {
						@Override
						public void run() {
					          Mainmenu_adapter.notifyDataSetChanged();
						}
					    });
				      
					

				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("TG", "JsonError:"+e.toString());
			}

			return null;
		}

		@Override
		protected void onPreExecute() {
			
			//adapter=null;
			//mSlideMenu.close(true);
			Mainmenu_MyArrList.clear();
		    
			if (Mainmenu_adapter == null) {
				Mainmenu_adapter = new MainmenuListAdapter(THIS, Mainmenu_MyArrList);
				Mainmenu_lstView.setAdapter(Mainmenu_adapter);
			}
			
			
			// =====================ProgressDialog==========================
			mProgressDialog = new ProgressDialog(THIS);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setMessage("Loading......");
			mProgressDialog.setCancelable(true);

			if (mProgressDialog != null)
			mProgressDialog.show();

			
			
			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

			Log.e("star", "progressupdate");
		}

		@Override
		protected void onPostExecute(Object result) {
			
			if(!ONcancle)
	    	{
			
			if (mProgressDialog != null)
				mProgressDialog.hide();
			//SetLayOut
	    	
				Mainmenu_lstView_params.height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Mainmenu_MyArrList.size()*50, THIS.getResources().getDisplayMetrics());
				ResizeWidthAnimation anim = new ResizeWidthAnimation(Mainmenu_lstView,Mainmenu_lstView.getWidth() ,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Mainmenu_MyArrList.size()*50, THIS.getResources().getDisplayMetrics()));
			    anim.setDuration(300);
			    Mainmenu_lstView.startAnimation(anim);
				 
				 
				 
					 
				 
				 Mainmenu_txt_title_menu.setText(Mainmenu_List_Menu_Title.get(Mainmenu_List_Menu_Title.size()-1));
	    	
				  if(Mainmenu_List_DATA_History.size()<=1)
					  THIS.findViewById(R.id.img_back_list).setVisibility(View.INVISIBLE);
			        else
			        	THIS.findViewById(R.id.img_back_list).setVisibility(View.VISIBLE);
			        
				   
				 
	    	
	    	}  
	    	
	    	
		}
	}
	private void Add_ChildToList(String Data,Boolean AddNewData) {
		//Log.e("TG", "Add_Child_To_List=:" + Data);
		
		if(AddNewData){
		MenuData menudata = new MenuData();
		menudata.Data = Data;
		menudata.Type = "child";
		Mainmenu_List_DATA_History.add(menudata);
		}
	

		// =================================
		Mainmenu_adapter = null;
		Mainmenu_MyArrList.clear();
		if (Mainmenu_adapter == null) {
			Mainmenu_adapter = new MainmenuListAdapter(THIS, Mainmenu_MyArrList);
			Mainmenu_lstView.setAdapter(Mainmenu_adapter);
		}
		try {
			JSONArray JSONarray = new JSONArray(Data);
			for (int i = 0; i < JSONarray.length(); i++) {

				JSONObject jsonObj_information = JSONarray.getJSONObject(i);
				Mainmenu_map = new HashMap<String, Object>();

				String tagName = "name" + (i );
				String tagId = "page_id" + (i);

				
				
				Mainmenu_map.put("Title", jsonObj_information.getString(tagName));
				Mainmenu_map.put("Id", jsonObj_information.getString(tagId));
				Mainmenu_map.put("Stuck", "child");

				try {
					JSONArray jsonarray_child = jsonObj_information
							.getJSONArray("child");
					if (jsonarray_child.length() > 1)
					{
						Mainmenu_map.put("Type", "main");
						Mainmenu_map.put("Data", jsonarray_child.toString());
					}
					else
					{
						Mainmenu_map.put("Type", "child");
						Mainmenu_map.put("Data", "");
					}
					
				} catch (Exception ex) {
					Mainmenu_map.put("Type", "child");
					Mainmenu_map.put("Data", "");
				}
				
				
//				if(JSONarray.length()<1)
//					Mainmenu_map.put("Type", "child");
//					Mainmenu_map.put("Data", "");

				Mainmenu_MyArrList.add(Mainmenu_map);
				THIS.runOnUiThread(new Runnable() {
		            @Override
		            public void run() {
                // This code will always run on the UI thread, therefore is safe to modify UI elements.
		            	Mainmenu_adapter.notifyDataSetChanged();
		            }
		        });
				
				
		    	//Mainmenu_lstView_params.height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Mainmenu_MyArrList.size()*50, THIS.getResources().getDisplayMetrics());
		    
				ResizeWidthAnimation anim = new ResizeWidthAnimation(Mainmenu_lstView,Mainmenu_lstView.getWidth() ,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Mainmenu_MyArrList.size()*50, THIS.getResources().getDisplayMetrics()));
			    anim.setDuration(300);
			    Mainmenu_lstView.startAnimation(anim);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Mainmenu_txt_title_menu.setText(Mainmenu_List_Menu_Title.get(Mainmenu_List_Menu_Title.size()-1));


	}
	private void Add_MainMenuToList(String Data,Boolean AddNewData) {
		Log.e("TG", "DATA Add main=:" + Data);

		// =================================
		Mainmenu_adapter = null;
		Mainmenu_MyArrList.clear();
		if (Mainmenu_adapter == null) {
			Mainmenu_adapter = new MainmenuListAdapter(THIS, Mainmenu_MyArrList);
			Mainmenu_lstView.setAdapter(Mainmenu_adapter);
		}
		try {
			JSONArray jsonarray_information = new JSONArray(Data);
			for (int i = 0; i < jsonarray_information.length(); i++) {

				JSONObject jsonObj_information = jsonarray_information
						.getJSONObject(i);

				Mainmenu_map = new HashMap<String, Object>();
				Mainmenu_map.put("Title", jsonObj_information.getString("main_menu"));
				Mainmenu_map.put("Id", jsonObj_information.getString("page_id"));
				Mainmenu_map.put("Stuck", "main_menu");

				try {
					JSONArray jsonarray_main_menu_child = jsonObj_information.getJSONArray("main_menu_child");
					if (jsonarray_main_menu_child.length() > 0)
						Mainmenu_map.put("Type", "main");
					else
						Mainmenu_map.put("Type", "child");

					Mainmenu_map.put("Data", jsonarray_main_menu_child.toString());

				} catch (Exception ex) {
					Mainmenu_map.put("Type", "child");
					Mainmenu_map.put("Data", "");
				}
		    	 
				Mainmenu_MyArrList.add(Mainmenu_map);
				
				THIS.runOnUiThread(new Runnable() {
		            @Override
		            public void run() {
                // This code will always run on the UI thread, therefore is safe to modify UI elements.
		            	Mainmenu_adapter.notifyDataSetChanged();
		            }
		        });
				//Mainmenu_lstView_params.height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Mainmenu_MyArrList.size()*50, THIS.getResources().getDisplayMetrics());
				ResizeWidthAnimation anim = new ResizeWidthAnimation(Mainmenu_lstView,Mainmenu_lstView.getWidth() ,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, Mainmenu_MyArrList.size()*50, THIS.getResources().getDisplayMetrics()));
			    anim.setDuration(300);
			    Mainmenu_lstView.startAnimation(anim);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Mainmenu_txt_title_menu.setText(Mainmenu_List_Menu_Title.get(Mainmenu_List_Menu_Title.size()-1));
		  
	}

	
	
	
	
	private ListView information_lstView;
	private informationListAdapter information_adapter;
	public  ArrayList<HashMap<String, Object>> information_MyArrList;
	private HashMap<String, Object> information_map = new HashMap<String, Object>();
	//private ProgressDialog mProgressDialog;
	private TextView information_txt_title_menu;
	private LayoutParams information_lstView_params;
	private void Setinformation()
	{
		
		
	
		
		
		information_lstView = (ListView)THIS.findViewById(R.id.list_view_information);
		information_lstView_params= information_lstView.getLayoutParams();
		THIS.setProgressBarIndeterminateVisibility(true);
		information_lstView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
				// TODO Auto-generated method stub

				 if(information_MyArrList.get(position).get("Data").toString().equals("")||
						 information_MyArrList.get(position).get("Data").toString().equals("[]")	)
			        {
					 try{
							
							if(!information_MyArrList.get(position).get("Id").equals(null))
							{
								
								Mainmenu_List_Menu_Title.add(information_MyArrList.get(position).get("Title").toString());
												
								
								Control.page_id=information_MyArrList.get(position).get("Id").toString();
								
								if(new ConnectInternet(THIS).checkInternetConnection())
								new LoadData_MainMenu().execute();
							}
						}
						catch(Exception ex){}
			        }
			        else
			        {
			        	information_List_Menu_Title.add(information_MyArrList.get(position).get("Title").toString());
						Add_information_ChildToList(information_MyArrList.get(position).get("Data").toString(),true);
						
			        }
				 
			
			
					 if(information_List_DATA_History.size()<=1)
						 THIS.findViewById(R.id.img_back_list_information).setVisibility(View.INVISIBLE);
						else
							THIS.findViewById(R.id.img_back_list_information).setVisibility(View.VISIBLE);
			
			
						Log.e("TG", "information_List_DATA_History size : "+information_List_DATA_History.size());

			}
		});
		

       
        information_txt_title_menu=(TextView)THIS.findViewById(R.id.txt_title_information);
        information_List_Menu_Title.add(information_page_title);
        information_txt_title_menu.setText(information_List_Menu_Title.get(0));
        
        
        
        
        
        
        if(information_List_DATA_History.size()<=2)
        	THIS.findViewById(R.id.img_back_list_information).setVisibility(View.INVISIBLE);
        else
        	THIS.findViewById(R.id.img_back_list_information).setVisibility(View.VISIBLE);
        
        THIS.findViewById(R.id.img_back_list_information).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(information_List_DATA_History.size()>1)
				{
					
					
					
					information_List_DATA_History.remove(information_List_DATA_History.size()-1);


					information_List_Menu_Title.remove(information_List_Menu_Title.size()-1);
					Log.e("TG", "Type : "+information_List_DATA_History.get(information_List_DATA_History.size()-1).Type);
					
					if(information_List_DATA_History.get(information_List_DATA_History.size()-1).Type.equals("child"))
						{
						
						Add_information_ChildToList(information_List_DATA_History.get(information_List_DATA_History.size()-1).Data,false);
						}
					else
					{
						
						Add_information_MainMenuToList(information_List_DATA_History.get(information_List_DATA_History.size()-1).Data,false);
					}
					
					
				}
				
				
				if(information_List_DATA_History.size()<=1)
					THIS.findViewById(R.id.img_back_list_information).setVisibility(View.INVISIBLE);
				
				Log.e("TG", "information_List_DATA_History size : "+information_List_DATA_History.size());
				
			}
		});
   
        information_MyArrList = new ArrayList<HashMap<String, Object>>();
        THIS.findViewById(R.id.Lay_information).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(new ConnectInternet(THIS).checkInternetConnection())
				{
					//new LoadData_MainMenu().execute();
					new LoadData_information().execute();
				}
			}
		});
		
		
		
	}

	
	
	
	private void Add_information_ChildToList(String Data,Boolean AddNewData) {
		Log.e("TG", "Add_Child_To_List=:" + Data);
		
		if(AddNewData){
		MenuData menudata = new MenuData();
		menudata.Data = Data;
		menudata.Type = "child";
		information_List_DATA_History.add(menudata);
		}
		Log.e("TG", "DATA=:" + Data);

		// =================================
		information_adapter = null;
		information_MyArrList.clear();
		if (information_adapter == null) {
			information_adapter = new informationListAdapter(THIS, information_MyArrList);
			information_lstView.setAdapter(information_adapter);
		}
		try {
			JSONArray JSONarray = new JSONArray(Data);
			for (int i = 0; i < JSONarray.length(); i++) {

				JSONObject jsonObj_information = JSONarray.getJSONObject(i);
				information_map = new HashMap<String, Object>();

				String tagName = "name" + (i + 1);
				String tagId = "page_id" + (i + 1);
				
				String tagName2 = "menu_title" + (i + 1);
				String tagId2 = "page_id" + (i + 1);
				

				try {information_map.put("Title", jsonObj_information.getString(tagName));} 
				catch (Exception ex) { 	try { information_map.put("Title", jsonObj_information.getString(tagName2));} catch (Exception ex2) {}}
				
				
				try {information_map.put("Id", jsonObj_information.getString(tagId));} 
				catch (Exception ex) {  try {information_map.put("Id", jsonObj_information.getString(tagId2));}catch (Exception ex2) {}}
				
				
				information_map.put("Stuck", "child");

				try {
					JSONArray jsonarray_child = jsonObj_information
							.getJSONArray("child");
					if (jsonarray_child.length() > 0)
						information_map.put("Type", "main");
					else
						information_map.put("Type", "child");

					information_map.put("Data", jsonarray_child.toString());
				} catch (Exception ex) {
					information_map.put("Type", "child");
					information_map.put("Data", "");
				}

				information_MyArrList.add(information_map);
				THIS.runOnUiThread(new Runnable() {
		            @Override
		            public void run() {
                // This code will always run on the UI thread, therefore is safe to modify UI elements.
		            		information_adapter.notifyDataSetChanged();
		            }
		        });
				information_lstView_params.height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, information_MyArrList.size()*50, THIS.getResources().getDisplayMetrics());

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		information_txt_title_menu.setText(information_List_Menu_Title.get(information_List_Menu_Title.size()-1));


	}
	private void Add_information_MainMenuToList(String Data,Boolean AddNewData) {
		Log.e("TG", "DATA Add main=:" + Data);

		// =================================
		information_adapter = null;
		information_MyArrList.clear();
		if (information_adapter == null) {
			information_adapter = new informationListAdapter(THIS, information_MyArrList);
			information_lstView.setAdapter(information_adapter);
		}
		try {
			JSONArray jsonarray_information = new JSONArray(Data);
			for (int i = 0; i < jsonarray_information.length(); i++) {

				JSONObject jsonObj_information = jsonarray_information
						.getJSONObject(i);

				String tagName = "name" + (i + 1);
				String tagId = "page_id" + (i + 1);
				
				information_map = new HashMap<String, Object>();
				try {information_map.put("Title", jsonObj_information.getString("main_menu"));} catch (Exception ex) {}
				try {information_map.put("Id", jsonObj_information.getString("page_id"));} catch (Exception ex) {}
				information_map.put("Stuck", "main_menu");

				try {
					JSONArray jsonarray_main_menu_child = jsonObj_information.getJSONArray("main_menu_child");
					if (jsonarray_main_menu_child.length() > 0)
						information_map.put("Type", "main");
					else
						information_map.put("Type", "child");

					information_map.put("Data", jsonarray_main_menu_child.toString());

				} catch (Exception ex) {
					information_map.put("Type", "child");
					information_map.put("Data", "");
				}
		    	 
				information_MyArrList.add(information_map);
				
				
				THIS.runOnUiThread(new Runnable() {
		            @Override
		            public void run() {
                // This code will always run on the UI thread, therefore is safe to modify UI elements.
		            		information_adapter.notifyDataSetChanged();
		            }
		        });
				
				
				information_lstView_params.height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, information_MyArrList.size()*50, THIS.getResources().getDisplayMetrics());

			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try{
		information_txt_title_menu.setText(information_List_Menu_Title.get(information_List_Menu_Title.size()-1));
		} catch (Exception ex){Log.e("ERROR", ex.toString());};
	}
	
	
	
	
	
	public class informationListAdapter extends BaseAdapter {	
	    private Activity activity;
	    private LayoutInflater inflater=null;
	    ArrayList<HashMap<String, Object>> MyArrList = new ArrayList<HashMap<String, Object>>();
	    
	    public informationListAdapter(Activity a,ArrayList al ) {
	    	
	    	activity = a;
	        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        MyArrList=al;
	       
	    }
	    
	    private class ViewHolder {
	    	private TextView txt_title;
			private ImageView img_arrow;
		}


	   
	    @Override
		public int getCount() {

	        return MyArrList.size();
	    }
	    @Override
	    public Object getItem(int position) {
	        return position;
	    }
	    @Override
	    public long getItemId(int position) {
	        return position;
	    }
	    
	    public View getView(final int position, View convertView, ViewGroup parent) {
	      
	    	View vi=convertView;
	    	final ViewHolder holder;
	    	
	        if(convertView==null)
	        {
		        vi = inflater.inflate(R.layout.item_mainmenu_listview2, null);
		        holder = new ViewHolder();
			   
			    holder.txt_title= (TextView)vi.findViewById(R.id.txt_title);
			    holder.img_arrow=(ImageView)vi.findViewById(R.id.img_arrow);
			   
			    
				vi.setTag(holder);	
		 	
		 		
		 		
		
		 		
		}else 
	    {
		
	    		holder = (ViewHolder)vi.getTag();
	    }
	            
	        holder.txt_title.setText(MyArrList.get(position).get("Title").toString());
	        if(	MyArrList.get(position).get("Data").toString().equals("")||
	        		MyArrList.get(position).get("Data").toString().equals("[]")	)
	        {
	        	holder.img_arrow.setVisibility(View.INVISIBLE);
	        }
	        else
	        	holder.img_arrow.setVisibility(View.VISIBLE);
	        //=================================SetData===================================

	        
		
		return vi;
	    }

	}
	class LoadData_information extends AsyncTask<Object, Integer, Object> {
		private boolean ONcancle=false;
		

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			ONcancle=true;
			super.onCancelled();
		}

		@SuppressWarnings("null")
		@Override
		protected Object doInBackground(Object... params) {

			
			try {
				
				JSONObject jsonObj=null;
			
				jsonObj = new JSONObject(HttpGetJson.GetJson(Control.API_Information,"",""));
				
				
				
				Log.e("tg", "LoadData_information:"+HttpGetJson.GetJson(Control.API_Information,"",""));
				
				JSONArray jsonarray_information=jsonObj.getJSONArray("information");
				
				// ===========Add to History
				MenuData menudata = new MenuData();
				menudata.Data = jsonObj.getJSONArray("information").toString();
				menudata.Type = "main";
				information_List_DATA_History.add(menudata);

				for (int i = 0; i < jsonarray_information.length(); i++) {

					JSONObject jsonObj_information = jsonarray_information
							.getJSONObject(i);

					
					information_map = new HashMap<String, Object>();
					try {information_map.put("Title", jsonObj_information.getString("main_menu"));} catch (Exception ex) {}
					try {information_map.put("Id", jsonObj_information.getString("page_id"));} catch (Exception ex) {}
					information_map.put("Stuck", "main_menu");
					
					
					Log.e("TG","Title:"+jsonObj_information.getString("main_menu"));
					

					try {
						JSONArray jsonarray_main_menu_child = jsonObj_information
								.getJSONArray("main_menu_child");
						if (jsonarray_main_menu_child.length() > 0)
							information_map.put("Type", "main");
						else
							information_map.put("Type", "child");

						information_map.put("Data", jsonarray_main_menu_child.toString());

					} catch (Exception ex) {
						information_map.put("Type", "child");
						information_map.put("Data", "");
					}

					
					information_MyArrList.add(information_map); 
					 
					
					 
					
					

				}
			
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
			
			return null;
		}

		@Override
		protected void onPreExecute() {

			
			information_MyArrList.clear();
		    
			if (information_adapter == null) {
				information_adapter = new informationListAdapter(THIS, information_MyArrList);
				information_lstView.setAdapter(information_adapter);
			}
			
			
			// =====================ProgressDialog==========================
			if(mProgressDialog == null)
			{
			mProgressDialog = new ProgressDialog(THIS);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setMessage("Loading......");
			mProgressDialog.setCancelable(true);
			}
			
			if (mProgressDialog != null)
				mProgressDialog.show();
			
			super.onPreExecute();
		}

		@Override
		public void onProgressUpdate(Integer... progress) {

		
			
			
		}

		@Override
		protected void onPostExecute(Object result) {
			
			if(!ONcancle)
			{
	    	 information_lstView_params.height=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, information_MyArrList.size()*50, THIS.getResources().getDisplayMetrics());
	    	 information_txt_title_menu.setText(information_List_Menu_Title.get(information_List_Menu_Title.size()-1));

			
			//if (mProgressDialog != null)
				mProgressDialog.hide();
		
				THIS.runOnUiThread(new Runnable() 
				{
					
					  @Override
					  public void run() {
			             // This code will always run on the UI thread, therefore is safe to modify UI elements.
					      
					       information_adapter.notifyDataSetChanged();
					   }
			  });
			}	
			
			Log.e("TG", "information_List_DATA_History size : "+information_List_DATA_History.size());
		}

	}

	
	
	
	
}
